const speedDiv = document.getElementById('speed');
const decreaseBtn = document.getElementById('decrease');
const increaseBtn = document.getElementById('increase');

let videoSpeed = 1; // 默认速度

chrome.tabs.query({active: true, currentWindow: true}, tabs => {
  chrome.tabs.sendMessage(tabs[0].id, {action: 'getSpeed'}, response => {
    videoSpeed = response.speed;
    updateSpeedDisplay();
  });
});

decreaseBtn.addEventListener('click', () => {
  videoSpeed = Math.max(videoSpeed - 0.25, 0.25);
  updateSpeed();
});

increaseBtn.addEventListener('click', () => {
  videoSpeed = Math.min(videoSpeed + 0.25, 16);
  updateSpeed();
});

function updateSpeed() {
  chrome.tabs.query({active: true, currentWindow: true}, tabs => {
    chrome.tabs.sendMessage(tabs[0].id, {action: 'setSpeed', speed: videoSpeed});
  });
  updateSpeedDisplay();
}

function updateSpeedDisplay() {
  speedDiv.textContent = `${videoSpeed}x`;
}